package com.memory.admin.earn.controller;

import com.memory.admin.system.validator.UserValid;
import com.memory.common.constant.AdminConst;
import com.memory.common.enums.ResultEnum;
import com.memory.common.exception.ResultException;
import com.memory.common.utils.EntityBeanUtil;
import com.memory.common.utils.ResultVoUtil;
import com.memory.common.vo.ResultVo;
import com.memory.component.actionLog.action.UserAction;
import com.memory.component.actionLog.annotation.ActionLog;
import com.memory.component.actionLog.annotation.EntityParam;
import com.memory.component.shiro.ShiroUtil;
import com.memory.modules.business.domain.earn.CurDayPrice;
import com.memory.modules.business.service.earn.CurDayPriceService;
import com.memory.modules.system.domain.User;
import lombok.RequiredArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.WebRequest;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @PackageName: com.memory.admin.earn.controller
 * @ClassName: CurDayPriceController
 * @Description:
 * @author: zhuguangxun
 * @date 2021/3/11
 */
@Controller
@RequestMapping("/business/cur/day/price")
@RequiredArgsConstructor
public class CurDayPriceController {
    @InitBinder
    public void initBinder(WebDataBinder binder, WebRequest request) {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }

    private final CurDayPriceService curDayPriceService;

    @GetMapping("list")
    public String list( Model model, CurDayPrice curDayPrice) {
        ExampleMatcher matcher = ExampleMatcher.matching();
        Example<CurDayPrice> example = Example.of(curDayPrice, matcher);
        final Page<CurDayPrice> page = curDayPriceService.findPage(example);
        model.addAttribute("list", page.getContent());
        model.addAttribute("page", page);
        return "/business/earn/curdayprice/list";
    }

    @GetMapping("add")
    public String toAdd() {
        return "/business/earn/curdayprice/add";
    }

    @RequestMapping("/delete")
    @ResponseBody
    public ResultVo delete(@RequestParam(value = "ids", required = false) Long id) {
        if (id != null) {
            curDayPriceService.remove(id);
            return ResultVoUtil.success("删除成功");
        }

        return ResultVoUtil.error("请传入删除的id");
    }

    /**
     * 保存添加/修改的数据
     */
    @PostMapping("/save")
    @ResponseBody
    public ResultVo save(@EntityParam CurDayPrice curDayPrice) {
        if (curDayPrice.getId() != null) {
            curDayPriceService.modify(curDayPrice.getId(), curDayPrice);
        } else {
            //去重
            CurDayPrice curDayPrice1 = curDayPriceService.findByDate(curDayPrice.getCreateDate());
            if (curDayPrice1 == null) {
                curDayPriceService.create(curDayPrice);
            } else {
                return ResultVoUtil.error("日期重复了");
            }

        }
        return ResultVoUtil.SAVE_SUCCESS;
    }

    /**
     * 跳转到编辑页面
     */
    @GetMapping("/edit/{id}")
    public String toEdit(@PathVariable("id") CurDayPrice curDayPrice, Model model) {
        CurDayPrice curDayPrice1 = curDayPriceService.findById(curDayPrice.getId()).get();
        model.addAttribute("curDayPrice", curDayPrice1);
        return "/business/earn/curdayprice/add";
    }

}
